/*
 * TableTransferHandler.java
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.gui.transfer;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import javax.swing.JComponent;
import javax.swing.JTable;
import org.tiberiumlabs.lailaps.actions.ActionFactory;
import org.tiberiumlabs.lailaps.gui.elements.ExceptionDialog;
import org.tiberiumlabs.lailaps.list.DownloadItem;
import org.tiberiumlabs.lailaps.list.DownloadsListModel;
import org.tiberiumlabs.lailaps.settings.Settings;

/**
 *
 * @author <a href="mailto:beast.tiberiumlabs@gmail.com">Beast</a>
 */
public class TableTransferHandler extends DropTransferHandler {

    @Override
    protected Transferable createTransferable(JComponent source) {
        JTable table = (JTable) source;
        int[] rows = table.getSelectedRows();
        DownloadsListModel model = (DownloadsListModel) table.getModel();
        String[] URIs = new String[rows.length];
        for (int i = 0; i < rows.length; i++) {
            URIs[i] = model.getItem(rows[i]).getUrl();
        }
        return new TableSelection(new TableSelectionBean(URIs, rows));
    }

    @Override
    public int getSourceActions(JComponent c) {
        return COPY_OR_MOVE;
    }

    @Override
    public boolean importData(JComponent source, Transferable t) {
        try {
            JTable table = (JTable)source;
            if (t.isDataFlavorSupported(TableSelectionBean.tableSelectionFlavor)) {
                TableSelectionBean importData = (TableSelectionBean)t.getTransferData(TableSelectionBean.tableSelectionFlavor);

                int[] rowsToMove = importData.getRows();
                int targetRow = table.getSelectedRow();

                // model IS NOT thread-safe, so need to move from ActionFactory
                ActionFactory.execute(new MoveItemsRunnable(targetRow, rowsToMove));

                return true;
            }
            if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                String importString = (String)t.getTransferData(DataFlavor.stringFlavor);
                importString = importString.replace(" ", ""); // TODO check why we need this regex, may be better just concat?
                JTable.DropLocation dropLocation = table.getDropLocation();
                if (dropLocation == null || dropLocation.getRow() < 0) {
                    ActionFactory.silentAddElement(new DownloadItem(importString), -1, Settings.getApplicationSettings().isAutostartDownloads());
                }
                else {
                    ActionFactory.silentAddElement(new DownloadItem(importString), dropLocation.getRow(), Settings.getApplicationSettings().isAutostartDownloads());
                    table.getSelectionModel().clearSelection();
                }
                return true;
            }
        } catch (UnsupportedFlavorException ufe) {
            new ExceptionDialog(ufe);
        } catch (IOException ioe) {
            new ExceptionDialog(ioe);
        }

        return false;
    }

    @Override
    protected void exportDone(JComponent source, Transferable data, int action) {
    }

    @Override
    public boolean canImport(JComponent c, DataFlavor[] flavors) {
        for (int i = 0; i < flavors.length; i++) {
            if (TableSelectionBean.tableSelectionFlavor.equals(flavors[i]) || DataFlavor.stringFlavor.equals(flavors[i])) {
                return true;
            }
        }
        return false;
    }
}
